/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.SoapFaultException;
/*     */ import com.zimbra.common.soap.SoapHttpTransport;
/*     */ import com.zimbra.common.soap.SoapProtocol;
/*     */ import com.zimbra.common.soap.SoapTransport;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AuthToken;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.ZimbraAuthToken;
/*     */ import com.zimbra.cs.client.LmcSession;
/*     */ import com.zimbra.cs.client.soap.LmcSearchRequest;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.cs.service.AuthProvider;
/*     */ import com.zimbra.soap.JaxbUtil;
/*     */ import com.zimbra.soap.type.AccountBy;
/*     */ import com.zimbra.soap.type.AccountSelector;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ import junit.framework.TestCase;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestAuthentication
/*     */   extends TestCase
/*     */ {
/*  48 */   private static String USER_NAME = "testauthentication";
/*  49 */   private static String PASSWORD = "test123";
/*     */   private Provisioning mProv;
/*     */   private Integer mMboxId;
/*     */   
/*     */   public void setUp()
/*     */     throws Exception
/*     */   {
/*  56 */     this.mProv = Provisioning.getInstance();
/*  57 */     cleanUp();
/*     */     
/*     */ 
/*  60 */     String address = TestUtil.getAddress(USER_NAME);
/*  61 */     Map<String, Object> attrs = new HashMap();
/*  62 */     attrs.put("zimbraMailHost", TestUtil.getDomain());
/*  63 */     attrs.put("cn", "Unit test temporary user");
/*  64 */     attrs.put("displayName", "Unit test temporary user");
/*  65 */     Account account = this.mProv.createAccount(address, PASSWORD, attrs);
/*  66 */     assertNotNull("Could not create account", account);
/*  67 */     this.mMboxId = Integer.valueOf(MailboxManager.getInstance().getMailboxByAccount(account).getId());
/*     */   }
/*     */   
/*     */   protected void tearDown() throws Exception
/*     */   {
/*  72 */     cleanUp();
/*     */   }
/*     */   
/*     */   private Account getAccount() throws Exception
/*     */   {
/*  77 */     String address = TestUtil.getAddress(USER_NAME);
/*  78 */     return Provisioning.getInstance().get(Key.AccountBy.name, address);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testAccessDeletedAccount()
/*     */     throws Exception
/*     */   {
/*  88 */     LmcSession session = TestUtil.getSoapSession(USER_NAME);
/*  89 */     LmcSearchRequest req = new LmcSearchRequest();
/*  90 */     req.setQuery("in:inbox");
/*  91 */     req.setSession(session);
/*  92 */     req.invoke(TestUtil.getSoapUrl());
/*     */     
/*     */ 
/*  95 */     Account account = getAccount();
/*  96 */     assertNotNull("Account does not exist", account);
/*  97 */     this.mProv.deleteAccount(account.getId());
/*     */     
/*     */     try
/*     */     {
/* 101 */       req.invoke(TestUtil.getSoapUrl());
/*     */     } catch (SoapFaultException ex) {
/* 103 */       assertTrue("Unexpected error: " + ex.getMessage(), ex.getMessage().indexOf("auth credentials have expired") >= 0);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testAccessInactiveAccount()
/*     */     throws Exception
/*     */   {
/* 115 */     LmcSession session = TestUtil.getSoapSession(USER_NAME);
/* 116 */     LmcSearchRequest req = new LmcSearchRequest();
/* 117 */     req.setQuery("in:inbox");
/* 118 */     req.setSession(session);
/* 119 */     req.invoke(TestUtil.getSoapUrl());
/*     */     
/*     */ 
/* 122 */     Account account = getAccount();
/* 123 */     this.mProv.modifyAccountStatus(account, "maintenance");
/*     */     
/*     */     try
/*     */     {
/* 127 */       req.invoke(TestUtil.getSoapUrl());
/*     */     } catch (SoapFaultException ex) {
/* 129 */       String substring = "auth credentials have expired";
/* 130 */       String msg = String.format("Error message '%s' does not contain '%s'", new Object[] { ex.getMessage(), substring });
/* 131 */       assertTrue(msg, ex.getMessage().contains(substring));
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testSimpleAuth()
/*     */     throws Exception
/*     */   {
/* 141 */     Account a = TestUtil.getAccount(USER_NAME);
/* 142 */     SoapHttpTransport transport = new SoapHttpTransport(TestUtil.getSoapUrl());
/* 143 */     AccountSelector acctSel = new AccountSelector(AccountBy.name, a.getName());
/* 144 */     com.zimbra.soap.account.message.AuthRequest req = new com.zimbra.soap.account.message.AuthRequest(acctSel, "test123");
/* 145 */     Element resp = transport.invoke(JaxbUtil.jaxbToElement(req, SoapProtocol.SoapJS.getFactory()));
/* 146 */     com.zimbra.soap.account.message.AuthResponse authResp = (com.zimbra.soap.account.message.AuthResponse)JaxbUtil.elementToJaxb(resp);
/* 147 */     String newAuthToken = authResp.getAuthToken();
/* 148 */     assertNotNull("should have received a new authtoken", newAuthToken);
/* 149 */     AuthToken at = ZimbraAuthToken.getAuthToken(newAuthToken);
/* 150 */     assertTrue("new auth token should be registered", at.isRegistered());
/* 151 */     assertFalse("new auth token should not be expired yet", at.isExpired());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void testAdminAuth()
/*     */     throws Exception
/*     */   {
/* 159 */     SoapHttpTransport transport = new SoapHttpTransport(TestUtil.getAdminSoapUrl());
/* 160 */     com.zimbra.soap.admin.message.AuthRequest req = new com.zimbra.soap.admin.message.AuthRequest(LC.zimbra_ldap_user.value(), LC.zimbra_ldap_password.value());
/* 161 */     Element resp = transport.invoke(JaxbUtil.jaxbToElement(req, SoapProtocol.SoapJS.getFactory()));
/* 162 */     com.zimbra.soap.admin.message.AuthResponse authResp = (com.zimbra.soap.admin.message.AuthResponse)JaxbUtil.elementToJaxb(resp);
/* 163 */     String newAuthToken = authResp.getAuthToken();
/* 164 */     assertNotNull("should have received a new authtoken", newAuthToken);
/* 165 */     AuthToken at = ZimbraAuthToken.getAuthToken(newAuthToken);
/* 166 */     assertTrue("new auth token should be registered", at.isRegistered());
/* 167 */     assertFalse("new auth token should not be expired yet", at.isExpired());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void testAdminAuthViaCookie()
/*     */     throws Exception
/*     */   {
/* 175 */     SoapHttpTransport transport = new SoapHttpTransport(TestUtil.getAdminSoapUrl());
/* 176 */     com.zimbra.soap.admin.message.AuthRequest req = new com.zimbra.soap.admin.message.AuthRequest(LC.zimbra_ldap_user.value(), LC.zimbra_ldap_password.value());
/* 177 */     Element resp = transport.invoke(JaxbUtil.jaxbToElement(req, SoapProtocol.SoapJS.getFactory()));
/* 178 */     com.zimbra.soap.admin.message.AuthResponse authResp = (com.zimbra.soap.admin.message.AuthResponse)JaxbUtil.elementToJaxb(resp);
/* 179 */     String newAuthToken = authResp.getAuthToken();
/* 180 */     assertNotNull("should have received a new authtoken", newAuthToken);
/* 181 */     AuthToken at = ZimbraAuthToken.getAuthToken(newAuthToken);
/* 182 */     assertTrue("new auth token should be registered", at.isRegistered());
/* 183 */     assertFalse("new auth token should not be expired yet", at.isExpired());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void testAdminAuthViaSOAPToken()
/*     */     throws Exception
/*     */   {
/* 191 */     AuthToken at = AuthProvider.getAdminAuthToken();
/* 192 */     SoapTransport transport = TestUtil.getAdminSoapTransport();
/* 193 */     com.zimbra.soap.admin.message.AuthRequest req = new com.zimbra.soap.admin.message.AuthRequest();
/* 194 */     req.setAuthToken(at.getEncoded());
/* 195 */     Element resp = transport.invoke(JaxbUtil.jaxbToElement(req, SoapProtocol.SoapJS.getFactory()));
/* 196 */     com.zimbra.soap.admin.message.AuthResponse authResp = (com.zimbra.soap.admin.message.AuthResponse)JaxbUtil.elementToJaxb(resp);
/* 197 */     String newAuthToken = authResp.getAuthToken();
/* 198 */     assertNotNull("should have received a new authtoken", newAuthToken);
/* 199 */     at = ZimbraAuthToken.getAuthToken(newAuthToken);
/* 200 */     assertTrue("new auth token should be registered", at.isRegistered());
/* 201 */     assertFalse("new auth token should not be expired yet", at.isExpired());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private void cleanUp()
/*     */     throws Exception
/*     */   {
/* 209 */     Account account = getAccount();
/* 210 */     if (account != null) {
/* 211 */       Provisioning.getInstance().deleteAccount(account.getId());
/*     */     }
/*     */     
/* 214 */     if (this.mMboxId != null) {
/* 215 */       Mailbox mbox = MailboxManager.getInstance().getMailboxById(this.mMboxId.intValue());
/* 216 */       mbox.deleteMailbox();
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestAuthentication.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */